package com.ecarx.module_network.bean;

import android.net.wifi.WifiManager;

import androidx.annotation.NonNull;

import com.ecarx.module_network.R;

import java.io.Serializable;


public class WifiInfo implements Serializable, Comparable<WifiInfo> {

    public WifiInfo() {

    }

    public WifiInfo(int type) {
        this.type = type;
    }

    /**
     * wifi类型
     */
    public interface Type {
        /**
         * 自定义类型wifi
         */
        int CONFIG = 1;

        /**
         * 扫描获取的wifi类型
         */
        int SCAN = 2;
        /**
         * 提示性文字，收起，查看更多网络
         */
        int TIP = 3;
    }

    public interface ConnectState {
        int CONNECTED = 0;
        int DISCONNECTED = 1;
        int CONNECTING = 2;
    }

    public interface Security {
        int NONE = 0;

        int WEP = 1;

        int PSK = 2;

        int EAP = 3;

        int WPA = 4;
    }

    public interface WifiLevel {
        int LEVEL_0 = 0;
        int LEVEL_1 = 1;
        int LEVEL_2 = 2;
        int LEVEL_3 = 3;
        int LEVEL_4 = 4;
    }

    private String ssid;
    private int connectState;
    private String bssid;
    private String password = "";

    public boolean isConnecting() {
        return isConnecting;
    }

    public void setConnecting(boolean connecting) {
        isConnecting = connecting;
    }

    private boolean isConnecting;
    private int level;

    private int security;
    private int type;

    public String getSsid() {
        return ssid;
    }

    public void setSsid(String ssid) {
        this.ssid = ssid;
    }

    public String getBssid() {
        return bssid;
    }

    public void setBssid(String bssid) {
        this.bssid = bssid;
    }

    public int getConnectState() {
        return connectState;
    }

    public void setConnectState(int connectState) {
        this.connectState = connectState;
    }

    public void setCapabilities(String capabilities) {
        setSecurity(capabilities);
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getSecurity() {
        return security;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getWifiLevelImage() {

        int level = WifiManager.calculateSignalLevel(this.level, 5);
        switch (level) {
            case WifiLevel.LEVEL_0:
                return getSecurity() == Security.NONE ? R.drawable.ic_wifi_signal_0 : R.drawable.ic_wifi_signal_cryp_0;
            case WifiLevel.LEVEL_1:
                return getSecurity() == Security.NONE ? R.drawable.ic_wifi_signal_1 : R.drawable.ic_wifi_signal_cryp_1;
            case WifiLevel.LEVEL_2:
                return getSecurity() == Security.NONE ? R.drawable.ic_wifi_signal_2 : R.drawable.ic_wifi_signal_cryp_2;
            case WifiLevel.LEVEL_3:
                return getSecurity() == Security.NONE ? R.drawable.ic_wifi_signal_3 : R.drawable.ic_wifi_signal_cryp_3;
            case WifiLevel.LEVEL_4:
                return getSecurity() == Security.NONE ? R.drawable.ic_wifi_signal_4 : R.drawable.ic_wifi_signal_cryp_4;
            default:
                return R.drawable.ic_wifi_signal_0;
        }
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int compareTo(@NonNull WifiInfo o) {
        if (this.level > o.level) {
            return -1;
        }
        if (this.level < o.level) {
            return 1;
        }
        return 0;
    }

    public void setSecurity(int security) {
        this.security = security;
    }

    public void setSecurity(String value) {
        this.security = Security.NONE;
        if (value.contains("EAP")) {
            this.security = Security.EAP;
        } else if (value.contains("PSK")) {
            this.security = Security.PSK;
        } else if (value.contains("WEP")) {
            this.security = Security.WEP;
        }

    }

    @Override
    public String toString() {
        int level = WifiManager.calculateSignalLevel(this.level, 5);
        return "WifiInfo{" +
                "ssid='" + ssid + '\'' +
                ", connectState=" + connectState +
                ", bssid='" + bssid + '\'' +
                ", password='" + password + '\'' +
                ", isConnecting=" + isConnecting +
                ", level=" + level +
                ", security=" + security +
                ", type=" + type +
                '}';
    }
}
